package com.travel.server.manager.impl.command;

import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.common.util.CollectionUtil;
import com.travel.common.util.DateUtil;
import com.travel.server.manager.impl.dal.CustomerDal;
import com.travel.server.manager.impl.dal.ProjectDal;
import com.travel.server.manager.impl.dal.SpecialDal;
import com.travel.server.manager.model.CustomerInfo;
import com.travel.server.manager.model.ProjectInfo;
import com.travel.server.manager.model.ProjectSpecial;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/4 10:53
 * @Desc:
 */
@Component
public class BaseImportCommand {

    @Autowired
    public SpecialDal specialDal;

    @Autowired
    public ProjectDal projectDal;

    @Autowired
    public CustomerDal customerDal;

    public final static String PROJECT_NOT_EXIST_PRE = "导入失败，以下项目信息-出行日期不存在：";

    protected ProjectSpecial querySpecial(String projectName, Date travelDate){
        ProjectInfo projectInfo = queryProject(projectName);
        if(projectInfo == null){
            return null;
        }
        if(travelDate == null){
            throw new WorkException(WorkCode.TRAVEL_DATE_NOT_BE_NULL);
        }
        SpecialDal.SpecialParam param = new SpecialDal.SpecialParam();
        param.setProjectIdList(Arrays.asList(projectInfo.getId()));
        param.setTravelDate(travelDate);
        List<ProjectSpecial> list = specialDal.selectByExample(param);
        ProjectSpecial special = CollectionUtil.getFirst(list);
        special.setOrderChargeId(projectInfo.getOrderChargeId());
        return special;
    }

    protected ProjectInfo queryProject(String projectName){
        if(StringUtils.isBlank(projectName)){
            throw new WorkException(WorkCode.PROJECT_NOT_BE_NULL);
        }
        ProjectDal.ProjectParam param = new ProjectDal.ProjectParam();
        param.setRealName(projectName);
        List<ProjectInfo> list = projectDal.selectByExample(param);
        return CollectionUtil.getFirst(list);
    }

    protected CustomerInfo queryCustomer(String name, String mobile){
        CustomerDal.CustomerParam param = new CustomerDal.CustomerParam();
        if(StringUtils.isNotBlank(name)) {
            param.setRealName(name);
        }
        if(StringUtils.isNotBlank(mobile)){
            param.setRealMobile(mobile);
        }
        List<CustomerInfo> list = customerDal.selectByExample(param);
        return CollectionUtil.getFirst(list);
    }

    protected String getValue(XSSFCell cell) {
        if (cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
            return String.valueOf(cell.getBooleanCellValue()).trim();
        } else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
            if(HSSFDateUtil.isCellDateFormatted(cell)){
                Date date = cell.getDateCellValue();
                try {
                    return DateUtil.YYYY_MM_DD.format(date);
                }catch (Exception e){
                    throw new WorkException(WorkCode.EXCEL_DATE_NOT_CORRECT);
                }
            }else{
                BigDecimal big=new BigDecimal(cell.getNumericCellValue());
                String value = big.toString();
                //解决1234.0  去掉后面的.0
                if(null!=value&&!"".equals(value.trim())){
                    String[] item = value.split("[.]");
                    if(1<item.length&&"0".equals(item[1])){
                        value=item[0];
                    }
                }
                return value;
            }
        }
        else {
            return String.valueOf(cell.getStringCellValue()).trim();
        }
    }
}
